// Copyright (c) 2009 Mike Haight, Andy Kipp
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include "Resources.h"

using namespace HandBanana::Core;
using namespace HandBanana::Utilities::Xml;
using namespace HandBanana::Utilities;


Texture* Resources::GetTexture(const std::wstring &argNamespace, const std::wstring &argTexture) 
{
    const std::wstring resourceName = argNamespace + L"." + argTexture;

    //if (mTextures[resourceName] == NULL)
    //{
    //    Logger::Warn(L"The resource manager does not contain the resource: " + argNamespace + L"." + argTexture);
    //    return NULL;
    //}
    //else
    //{
    //    // Get stream
    //    std::wstringstream* stream = mTextures[resourceName];

    //    // Create XmlDocument
    //    XmlDocument* doc = new XmlDocument();

    //    // Load XmlDocument
    //    doc->Load(stream);

    //    // Get Texture XmlElement
    //    XmlElement* textureElement = (XmlElement*)doc->GetDocumentElement();

    //    // Get Texture XmlElement Attributes
    //    std::wstring name = textureElement->GetAttribute(L"name")->GetValue();
    //    int height = wstrparseint(textureElement->GetAttribute(L"height")->GetValue());
    //    int width = wstrparseint(textureElement->GetAttribute(L"width")->GetValue());
    //    int frameHeight = wstrparseint(textureElement->GetAttribute(L"frameHeight")->GetValue());
    //    int frameWidth = wstrparseint(textureElement->GetAttribute(L"frameWidth")->GetValue());

    //    // Create Texture
    //    Texture* texture = new Texture(name, width, height, frameWidth, frameHeight);

    //    // Delete XmlDocument
    //    SAFE_DELETE(doc);

    //    // Return Texture
    //    return texture;
    //}

    return NULL;
}

void Resources::LoadTextures(const std::wstring &argFileName)
{
    // Create XmlDocument
    XmlDocument* doc = new XmlDocument();

    // Load XmlDocument
    doc->Load(argFileName);

    // Get Textures XmlElement
    XmlElement* texturesElement = (XmlElement*)doc->GetDocumentElement();

    // Get Textures XmlElement Attributes
    std::wstring textureNamespace = texturesElement->GetAttribute(L"namespace")->GetValue();

    //// Read Textures Element
    //{
    //    XmlNode *childNode;
    //    for(childNode = texturesElement->GetFirstChild(); childNode != NULL; childNode = texturesElement->GetNextChild())
    //    {
    //        if (childNode->GetType() == Element)
    //        {
    //            XmlElement* childElement = (XmlElement*)childNode;

    //            if (childElement->GetName() == L"Texture")
    //            {
    //                // Write XmlElement to stream
    //                std::wstringstream* stream = new std::wstringstream();
    //                XmlWriter writer(stream);
    //                writer.WriteElement(childElement);

    //                // Get ResourceName
    //                const std::wstring resourceName = textureNamespace + L"." + childElement->GetAttribute(L"name")->GetValue();

    //                // Save stream
    //                mTextures[resourceName] = stream;
    //            }
    //            else
    //            {
    //                throw XmlException(L"Unknown Element: \"" + childElement->GetName() + L"\"");
    //            }
    //        }
    //    }
    //}

    // Delete XmlDocument
    SAFE_DELETE(doc);
}